博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Relay的麻烦之处
阅读量:4610 次
发布时间:2019-06-09

本文共 2697 字,大约阅读时间需要 8 分钟。

问题背景

由于QueryRender是直接将数据塞进Render()里的

handleUpdate = (hasNextPage, xdata) =>{    console.log(3);    const data = this.state.data.concat(xdata);    this.setState({      data: data,      loadingMore: false,      hasNextPage: hasNextPage      }, () => {        window.dispatchEvent(new Event('resize'));      });  }  render(){    return(      
{ if (error) { console.log(error) } if (!props) { return (
) } this.handleUpdate(props.bookList.hasNextPage, props.bookList.edges); const loadMore = this.state.hasNextPage ? (
{this.state.loadingMore &&
} {!this.state.loadingMore &&
}
) : null; const mydata = this.state.data.concat(props.bookList.edges); return (
) }} /> ) }

直接在render里进行setState会导致组件无限循环渲染,当然把queryrender取缔掉用fetch替换可以解决,但是怎么在使用relay的同时直接setState呢?

改进一:

export default class SearchList extends PureComponent{  state={    after: "",    data: [],  }  updateAfter = (after, xdata) =>{    const data = this.state.data.concat(xdata);    this.setState({after: after, data: data},    () =>{      window.dispatchEvent(new Event('resize'));    });  }  render(){    return(      
{ if (error) { console.log(error) } return (
this.updateAfter(props.bookList.pageInfo.endCursor, props.bookList.edges)} hasNextPage={props ? props.bookList.pageInfo.hasNextPage : null} dataSource={props ? this.state.data.concat(props.bookList.edges) : this.state.data}/> ) }} /> ) }}class SearchListComponent extends PureComponent{ constructor(props){ super(props) } componentWillReceiveProps = (nextProps) =>{ console.log(1) window.dispatchEvent(new Event('resize')); } render(){ const loadMore = this.props.hasNextPage ? (
{this.props.loadingMore &&
} {!this.props.loadingMore &&
}
) : null; return(
(
}>
{item.node.summary.replace(/
/g, ' ')}
{item.node.author}
|
{item.node.clickTimes} 点击
} />
)} /> ) } }

缺陷:点击加载更多会闪一下,因为render会走两遍,第一遍是加载中,return null

转载于:https://www.cnblogs.com/jiajin/p/8556113.html

你可能感兴趣的文章
[转]DELPHI——调试(1)
查看>>
JS秒数转成分秒时间格式
查看>>
xp_cmdshell 命令的开启与关闭,和状态查询
查看>>
Linux sudoers
查看>>
MySQL详解(18)-----------分页方法总结
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
队列实现霍夫曼树
查看>>
JAVA 笔记(一)
查看>>
{Nodejs} request URL 中文乱码
查看>>
异常及日志使用与项目打包
查看>>
数组相关函数
查看>>
Python 和其他编程语言数据类型的比较
查看>>
T2695 桶哥的问题——送桶 题解
查看>>
HTML5 表单
查看>>
Android群英传》读书笔记 (3) 第六章 Android绘图机制与处理技巧 + 第七章 Android动画机制与使用技巧...
查看>>
关于微信公众平台测试号配置失败的问题
查看>>
【NOIP2001】统计单词个数
查看>>